cmake_minimum_required(VERSION 3.10)
project(AirCombatDemo VERSION 1.0)


if(WIN32)
    # 基础配置
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)

    # 使用 MSVC 时的警告级别
    if(MSVC)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /EHsc")
    else()
        # MinGW 的设置
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
    endif()
    
    # Windows 下不需要 -fPIC
    add_definitions(-D_WINDOWS)

    set(Python_EXECUTABLE "E:/u_environment/anaconda/envs/Air_Battle/python.exe")
    set(Python_EXECUTABLE "C:/Users/an185/.conda/envs/Combat/python.exe")
    # Python 配置
    find_package(Python 3.9 COMPONENTS Interpreter Development REQUIRED)

    # 查找 pybind11
    set(pybind11_DIR "E:/u_environment/anaconda/envs/Air_Battle/Lib/site-packages/pybind11/share/cmake/pybind11")
    set(pybind11_DIR "C:/Users/an185/.conda/envs/Combat/Lib/site-packages/pybind11/share/cmake/pybind11")
    find_package(pybind11 REQUIRED)

    # 查找 Eigen 库
    set(EIGEN3_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src/Tools/eigen337)

    # 设置主包含目录
    include_directories(
        ${PROJECT_SOURCE_DIR}/include
        ${PROJECT_SOURCE_DIR}/src
        ${EIGEN3_INCLUDE_DIR}
        ${pybind11_INCLUDE_DIRS}
    )

    # 定义各模块库
    function(add_module_library name)
        file(GLOB sources ${PROJECT_SOURCE_DIR}/src/${name}/*.cpp)
        add_library(${name} STATIC ${sources})
        target_include_directories(${name} PUBLIC 
            ${PROJECT_SOURCE_DIR}/src/${name}
        )
        # Windows 下不需要 -fPIC
    endfunction()

    # 添加各模块（按依赖顺序）
    add_module_library(CombatSimulation)
    add_module_library(FlyTac)
    add_module_library(NEU)
    add_module_library(Sensor)
    add_module_library(TacView)

    # Tools 需要特殊处理
    set(TOOLS_SOURCES
        ${PROJECT_SOURCE_DIR}/src/Tools/coordinate.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/extern.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/interface.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/tool_function.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/planes_config.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/pugixml/pugixml.cpp
    )
    add_library(Tools STATIC ${TOOLS_SOURCES})
    target_include_directories(Tools PUBLIC ${PROJECT_SOURCE_DIR}/src/Tools)

    # 设置依赖关系
    target_link_libraries(CombatSimulation PRIVATE FlyTac)
    target_link_libraries(NEU PRIVATE CombatSimulation)
    target_link_libraries(Tools PRIVATE TacView)

    # 设置构建类型
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Debug)
    endif()

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(PYTHON_BUILD_DIR ${PROJECT_SOURCE_DIR}/build/build_win_debug/python_build)
    else()
        set(PYTHON_BUILD_DIR ${PROJECT_SOURCE_DIR}/build/build_win_release/python_build)
    endif()

    file(MAKE_DIRECTORY ${PYTHON_BUILD_DIR})


    # 设置 Python 模块属性
    pybind11_add_module(AirCombat_demo ${PROJECT_SOURCE_DIR}/src/AirCombat_demo.cpp)

    # Windows 下的输出配置
    set_target_properties(AirCombat_demo PROPERTIES SUFFIX ".pyd")
    
    # 设置输出目录

    foreach(CONFIG DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
        string(TOLOWER ${CONFIG} CONFIG_LOWER)
        set_target_properties(AirCombat_demo PROPERTIES
            OUTPUT_NAME_${CONFIG} "AirCombat_demo"
            PREFIX ""
        )
        set_target_properties(AirCombat_demo PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/build/build_win_debug/python_build
        LIBRARY_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/build/build_win_release/python_build
        )
    endforeach()
    
    # 添加 Windows 特定的链接库
    if(MSVC)
        target_link_options(AirCombat_demo PRIVATE "/DEBUG:FULL")
    endif()

    # 配置编译选项
    if(MSVC)
        target_compile_options(AirCombat_demo PRIVATE
            $<$<CONFIG:Debug>:/Od /Zi /RTC1>
            $<$<CONFIG:Release>:/O2 /Ob2 /DNDEBUG>
        )
    else()
        target_compile_options(AirCombat_demo PRIVATE
            $<$<CONFIG:Debug>:-O0 -g>
            $<$<CONFIG:Release>:-O3 -DNDEBUG>
        )
    endif()

    # 链接所有需要的库到 Python 模块
    target_link_libraries(AirCombat_demo PRIVATE
        NEU
        Sensor
        TacView
        Tools
        FlyTac
        CombatSimulation
        pybind11::module
        ${Python_LIBRARIES}
    )

    # Windows 下可能需要额外链接这些库
    if(WIN32)
        target_link_libraries(AirCombat_demo PRIVATE
            Shlwapi.lib
            Advapi32.lib
        )
    endif()
else()
    # 基础配置
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fPIC")  # 添加-fPIC用于生成位置无关代码

    set(Python_EXECUTABLE "/home/anjian/Desktop/enter/envs/Combat/bin/python")
    set(Python_LIBRARY "/home/anjian/Desktop/enter/envs/Combat/lib/libpython3.9.so")

    # 启用 Python 模块查找
    find_package(Python COMPONENTS Interpreter Development REQUIRED)

    # 查找pybind11
    set(pybind11_DIR "/home/anjian/Desktop/enter/envs/Combat/lib/python3.9/site-packages/pybind11/share/cmake/pybind11")
    find_package(pybind11 REQUIRED)

    # 查找Eigen库
    set(EIGEN3_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src/Tools/eigen337)

    # 设置主包含目录
    include_directories(
        ${PROJECT_SOURCE_DIR}/include
        ${PROJECT_SOURCE_DIR}/src
        ${Eigen3_INCLUDE_DIR}
        ${pybind11_INCLUDE_DIRS}  # 添加pybind11头文件路径
    )

    # 定义各模块库
    function(add_module_library name)
        file(GLOB sources ${PROJECT_SOURCE_DIR}/src/${name}/*.cpp)
        add_library(${name} STATIC ${sources})
        target_include_directories(${name} PUBLIC 
            ${PROJECT_SOURCE_DIR}/src/${name}
        )
        # 为所有模块添加-fPIC编译选项
        set_target_properties(${name} PROPERTIES POSITION_INDEPENDENT_CODE ON)
    endfunction()

    # 添加各模块（按依赖顺序）
    add_module_library(CombatSimulation)
    add_module_library(FlyTac)
    add_module_library(NEU)
    add_module_library(Sensor)
    add_module_library(TacView)

    # Tools需要特殊处理
    set(TOOLS_SOURCES
        ${PROJECT_SOURCE_DIR}/src/Tools/coordinate.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/extern.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/interface.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/tool_function.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/planes_config.cpp
        ${PROJECT_SOURCE_DIR}/src/Tools/pugixml/pugixml.cpp
    )
    add_library(Tools STATIC ${TOOLS_SOURCES})
    target_include_directories(Tools PUBLIC ${PROJECT_SOURCE_DIR}/src/Tools)
    set_target_properties(Tools PROPERTIES POSITION_INDEPENDENT_CODE ON)

    # 设置依赖关系
    target_link_libraries(CombatSimulation PRIVATE FlyTac)
    target_link_libraries(NEU PRIVATE CombatSimulation)
    target_link_libraries(Tools PRIVATE TacView)

    # 设置构建类型
    if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Debug)
    endif()


    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(PYTHON_BUILD_DIR ${PROJECT_SOURCE_DIR}/build/build_linux_debug/python_build)
    else()
        set(PYTHON_BUILD_DIR ${PROJECT_SOURCE_DIR}/build/build_linux_release/python_build)
    endif()

    file(MAKE_DIRECTORY ${PYTHON_BUILD_DIR})


    # 设置Python模块属性
    pybind11_add_module(AirCombat_demo ${PROJECT_SOURCE_DIR}/src/AirCombat_demo.cpp)

    # 配置输出目录
    foreach(CONFIG DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
        string(TOLOWER ${CONFIG} CONFIG_LOWER)
        set_target_properties(AirCombat_demo PROPERTIES
            OUTPUT_NAME_${CONFIG} "AirCombat_demo"
            SUFFIX ".so"
            PREFIX ""
            LIBRARY_OUTPUT_DIRECTORY_${CONFIG} "${PROJECT_SOURCE_DIR}/build/build_linux_${CONFIG_LOWER}/python_build"
        )
    endforeach()
    # 配置编译选项
    target_compile_options(AirCombat_demo PRIVATE
        $<$<CONFIG:Debug>:-O0 -g -fno-inline>
        $<$<CONFIG:Release>:-O3 -DNDEBUG -flto>
    )


    # 链接所有需要的库到Python模块
    target_link_libraries(AirCombat_demo PRIVATE
        NEU
        Sensor
        TacView
        Tools
        FlyTac
        CombatSimulation
        pybind11::module
        ${CMAKE_DL_LIBS}
    )

endif()